
let query = (location.search).substring(1);
// let query = window.location.href.split('?')[1]
query = decodeURI(query) 
let vars = query.split("&");
console.log(vars);                // ['title=数码相机', 'categoryid=11']
let item = vars[1].split("=")[1]
let item1 = vars[0].split("=")[1];
let aObj = document.querySelector('.oa')
aObj.innerHTML = `${item1}>`


let pageid
var totalPage = null
let timer = null;


let arr = function(pageid){

    clearTimeout(timer)
    timer = setTimeout(() => {

        const xhr = new XMLHttpRequest();
        xhr.open("get",`http://chst.vip:1234/api/getproductlist?categoryid=${item}`);
        xhr.send(null);
        xhr.onreadystatechange = function(){
            let oul = document.querySelector("ul");
            if(xhr.readyState === 4){
                if(xhr.status === 200){
                    let res = JSON.parse(xhr.responseText);
                    // console.log(res)
                    // let regExp = /(?<=imgurl=)[^'"]*/;
                    let ele = "";
                    res.result.forEach(item => {
                        // console.log(item);
                        var img = item.productImg;
                        // console.log(img);
                        ele += `<li>
                                    <a href="./bijiaxq.html?categoryid=${item.categoryId}&productid=${item.productId}&brandname=${item.brandName}">
                                        ${img}
                                        <div>
                                            <h4>${item.productName}</h4>
                                            <span>${item.productPrice}</span>
                                            <br>
                                            <p>${item.productQuote}</p>
                                            <i>${item.productCom}</i>
                                        </div>
                                    </a>
                                </li>`
                    });
                    oul.innerHTML = ele;
                }
            }
        };

        const xhr1 = new XMLHttpRequest();
        xhr1.open("get",`http://chst.vip:1234/api/getproductlist?pageid=${pageid}`);
        xhr1.send(null);
        xhr1.onreadystatechange = function(){
            if(xhr1.readyState === 4){
                if(xhr1.status === 200){
                    let res1 = JSON.parse(xhr.responseText);
                    console.log(res1)
                    

                    totalPage = Math.ceil(res1.totalCount/res1.pagesize)
                    console.log(totalPage);
                    $("#changePage .add").click(function(){
                        console.log(pageid);
                        if(pageid <=0 ){
                            return 
                        }
                        arr(--pageid)
                    });
                    $("#changePage .red").click(function(){
                        console.log(pageid);
                        if(pageid >3 ){
                            return 
                        }
                        arr(++pageid)
                    });
                    
                    let el = ''
                    let i = 0
                    // console.log(totalPage);
                    let opt = document.querySelector('#pageSelect')
                    // console.log(opt);
                    while(i < totalPage) {
                        el +=`<option value="${++i}">${i}/${totalPage} </option>`;
                    }
                    opt.innerHTML = el;

                }
            }
        };

    }, 500);
}
arr(0);




